package com.hina.rec.application.manager.system;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hina.rec.domain.mapper.RoleMapper;
import com.hina.rec.domain.mapper.UserRoleMapper;
import com.hina.rec.domain.model.RoleDO;
import com.hina.rec.domain.model.UserRoleDO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 角色管理Manager
 *
 * @author liaosp
 * @date 2024-01-09
 */
@Component
public class RoleManager extends ServiceImpl<RoleMapper, RoleDO> {

    @Resource
    private UserRoleMapper userRoleMapper;


    /**
     * 根据用户ID查询用户角色
     *
     * @param userId 用户ID
     * @return 角色列表
     */
    public List<RoleDO> getRolesByUserId(Integer userId) {
        // 根据用户ID查询用户角色ID
        List<Integer> roleIds = userRoleMapper.selectList(Wrappers.<UserRoleDO>lambdaQuery()
                        .eq(UserRoleDO::getUserId, userId))
                .stream()
                .map(UserRoleDO::getRoleId)
                .toList();
        if (CollUtil.isEmpty(roleIds)) {
            return List.of();
        }
        return listByIds(roleIds);
    }
}